package com.tang.excel.oci.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.tang.excel.oci.domain.MaterialConsumption;
import com.tang.excel.oci.mapper.MaterialConsumptionMapper;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Tang
 * @classname ExcelReadListener
 * @description [  ]
 * @date 2021/3/24 9:11
 */
@Slf4j
public class MaterialConsumptionExcelReadListener extends AnalysisEventListener<MaterialConsumption> {

    private List<MaterialConsumption> materialConsumptionList = new ArrayList<>(16);

    MaterialConsumptionMapper materialConsumptionMapper;

    public MaterialConsumptionExcelReadListener(MaterialConsumptionMapper materialConsumptionMapper) {
        this.materialConsumptionMapper = materialConsumptionMapper;
    }

    /**
     * 每解析一条数据都会调用
     * @param analysisContext
     */
    @Override
    public void invoke(MaterialConsumption materialConsumption, AnalysisContext analysisContext) {
        materialConsumption.setCreateTime(new Date());
        materialConsumption.setUpdateTime(new Date());
        materialConsumption.setOperatorUser("admin");
        materialConsumptionList.add(materialConsumption);
    }

    /**
     * 所有数据解析完成了 都会来调用
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("所有数据解析完成");
        materialConsumptionMapper.batchInsert(materialConsumptionList);
    }
}